'''MarysTicTacToe.py
test program for harness.

'''
 
def myNameIs():
   return "Mary's awesome Tic-Tac-Toe player"

def myCreatorIs():
   return "Mary Jackson"

def makeMove(currentState):
   mySide = currentState[0]
   global utterance
   utterance = 'Well, I do hope that '+mySide+' will win.'
   ''' Now here is where the new state should be computed.
     The following code is a placeholder and should be replaced
     by something that makes a better move.'''
   for s in successors(currentState):
      print s
   newState = findFirstMove(currentState)
   ''' End of the placeholder code'''
   return newState

def other(player):
   if player=='X': return 'O'
   else: return 'X'

def myUtterance():
   global utterance
   return utterance

def findFirstMove(currentState):
   mySide = currentState[0]
   board = currentState[1]
   for i in range(3):
      for j in range(3):
         if board[i][j] == ' ':
            board[i][j]=mySide
            return [other(mySide), board]
   global utterance
   utterance = "I give up!"
   return -1


def topRowWinForX(state):
    for cell in state[1][0][:]:
        if cell != 'X': return False
    return True

def anyWinForX(state, describe=False):
    return anyWinHelper("X", state, describe)

def successors(currentState):
   mySide = currentState[0]
   board = currentState[1]
   childrenState = []
   tempState = []
   for i in range(3):
      for j in range(3):
         if board[i][j] == ' ':
            tempState = [board[0][:],board[1][:],board[2][:]]
            tempState[i][j]=mySide
            childrenState.append([other(mySide),tempState])
   return childrenState

def chooseMove(listOfStates):
   for state in listOfStates:
      mySide = other(state[0])
      board = state[1]
      if anyWinHelper(mySide, state): return state
   return listOfStates[0]
      

def anyWinForO(state, describe=False):
    return anyWinHelper("O", state, describe)

def anyWinHelper(symbol, state, describe):
    board = state[1]
    global utterance
   
    #chekc 1st row
    if containAll(symbol,board[0][:]):
        if describe: utterance = "%s wins in the top row" % (symbol)
        return True
    #chekc 2nd row
    if containAll(symbol,board[1][:]):
        if describe: utterance = "%s wins in the second row" % (symbol)
        return True
    #chekc 3rd row
    if containAll(symbol,board[2][:]):
        if describe: utterance = "%s wins in the third row" % (symbol)
        return True
    #check 1st column
    if containAll(symbol,board[:][0]):
        if describe: utterance = "%s wins in the top column" % (symbol)
        return True
    #check 2nd column
    if containAll(symbol,board[:][1]):
        if describe: utterance = "%s wins in the second column" % (symbol)
        return True
    #check 3rd column
    if containAll(symbol,board[:][2]):
        if describe: utterance = "%s wins in the third column" % (symbol)
        return True
    #check diagonal
    if containAll(symbol,[board[0][0],board[1][1],board[2][2]]):
        if describe: utterance = "%s wins in the backward diagonal" % (symbol)
        return True
    if containAll(symbol,[board[2][0],board[1][1],board[0][2]]):
        if describe: utterance = "%s wins in the forward diagonal" % (symbol)
        return True
    return False


def containAll(v, l):
    for cell in l:
        if cell != v: return False
    return True 

